# @Version        : 1.0
# @Update Time    : 2024/9/10 21:16
# @File           : user_schema.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息
from datetime import datetime

from pydantic import Field, field_validator
from typing import Literal, Annotated

from src.schemas.post_schema import PostOut
from src.schemas.role_schema import RoleOut, RoleInfo
from src.schemas.dept_schema import DeptCreate, DeptUpdate
from src.schemas.base_schema import MixinSchema, custom_error_msg
from fastapi_utils.api_model import APIModel
from fastapi import Depends, Query
from src.core.dependencies import QueryParams, Paging
from src.core.query_builder import FilterCriteria, QueryType


class UserParams(QueryParams):
    def __init__(
        self,
        username: str = Query(None, description="用户名"),
        nickname: str = Query(None, description="昵称"),
        status: Literal["1", "2"] = Query(None, description="帐号状态"),
        phone: str = Query(None, description="电话"),
        sex: Literal["0", "1", "2"] = Query(None, description="用户性别"),
        params: Paging = Depends(Paging),
    ):
        super().__init__(params)
        self.username = FilterCriteria(QueryType.LIKE, username)
        self.nickname = FilterCriteria(QueryType.LIKE, nickname)
        self.status = status
        self.phone = FilterCriteria(QueryType.LIKE, phone)
        self.sex = sex


class UserBase(APIModel):
    nickname: str | None = Field(..., max_length=20, description="昵称")
    avatar: str | None = Field(None, max_length=255, description="头像")
    email: str | None = Field(None, max_length=64, description="邮箱")
    sex: str | None = Field(default="2", description="用户性别（0男 1女 2秘密）")
    phone: str | None = Field(None, max_length=11, description="电话")
    remark: str | None = Field(None, max_length=255, description="描述")


class UserSchema(UserBase):
    username: str = Field(..., max_length=20, description="用户名")
    is_super: Literal["0", "1"] = Field(default="0", description="（0普通 1管理员）")
    status: Literal["1", "2"] = Field(default="1", description="是否启用1:启用，2:禁用")
    login_ip: str | None = Field(None, max_length=30, description="ip地址")
    dept_id: int | None = Field(None, description="部门ID")
    # 岗位id集合
    post_ids: Annotated[
        list[int],
        Field(default=None, min_length=1, description="岗位id集合"),
        custom_error_msg("请选择岗位"),
    ]
    # 角色id集合
    role_ids: Annotated[
        list[int],
        Field(default=None, min_length=1, description="角色id集合"),
        custom_error_msg("请选择角色"),
    ]


class UserCreate(UserSchema):
    password: Annotated[
        str,
        Field(min_length=6, max_length=16, description="密码"),
        custom_error_msg("密码长度在6-16位"),
    ] = "123456"


class UserUpdate(UserSchema):
    id: int = Field(description="主键ID")


class UserOut(MixinSchema, UserSchema):
    roles: list[RoleOut] | None = Field(default=None, description="角色集合")
    posts: list[PostOut] | None = Field(default=None, description="岗位集合")
    dept: DeptCreate = Field(default=None, description="部门对象")


class User(UserOut):
    roles: list[RoleOut] | None = Field(default=None, description="角色集合")
    posts: list[PostOut] | None = Field(default=None, description="岗位集合")
    dept: DeptCreate = Field(default=None, description="部门对象")


class UserResetPassword(APIModel):
    id: int = Field(description="主键ID")
    password: Annotated[
        str,
        Field(min_length=6, max_length=16, description="密码"),
        custom_error_msg("密码长度在6-16位"),
    ] = "123456"


# 修改密码
class UserUpdatePassword(APIModel):
    old_password: Annotated[
        str,
        Field(min_length=6, max_length=16, description="旧密码"),
        custom_error_msg("旧密码长度在6-16位"),
    ] = "123456"
    new_password: Annotated[
        str,
        Field(min_length=6, max_length=16, description="新密码"),
        custom_error_msg("新密码长度在6-16位"),
    ] = "123456"


class OnlineUserParams(QueryParams):
    def __init__(
        self,
        params: Paging = Depends(Paging),
    ):
        super().__init__(params)


# 在线用户基本信息（不包含角色、部门、岗位等敏感信息）
class SimpleOnlineUser(APIModel):
    id: int = Field(description="用户ID")
    username: str = Field(description="用户名")
    nickname: str | None = Field(None, description="昵称")
    avatar: str | None = Field(None, description="头像")
    email: str | None = Field(None, description="邮箱")
    phone: str | None = Field(None, description="电话")
    sex: str | None = Field(None, description="性别")
    status: Literal["1", "2"] = Field(description="状态")
    is_super: Literal["0", "1"] = Field(description="是否超级管理员")
    device_count: int = Field(description="设备数量")
    updated_at: str = Field(description="更新时间")


# 在线用户详细信息（用于设备列表）
class OnlineUserOut(APIModel):
    login_time: str | None = Field(default=None, description="登录时间")
    last_active_time: str | None = Field(default=None, description="最后活跃时间")
    token_id: str | None = Field(default=None, description="会话令牌")
    device_type: str | None = Field(default=None, description="设备类型")
    ip: str | None = Field(default=None, description="登录IP")
    browser: str | None = Field(default=None, description="浏览器")
    os: str | None = Field(default=None, description="操作系统")
